/**
 * Created by acer1 on 2017/7/24.
 */
$(function () {

    add_resume();//添加简历
    edit_resume_lists();//编辑简历列表

});

/***********************************添加(编辑)简历start***********************************/
function add_resume() {
    var element_small = $('<small class="glyphicon glyphicon-edit"></small>');
    $('.click-edit').on('click', function () {
        $(this).html('保存');
        $(this).prev().find('input,textarea').attr('disabled', false);
        $(this).prev().find('input,textarea')[0].focus();
        $(this).prev().find('input,textarea').addClass('edit-active');

        $(this).off();//先把click事件清除掉
        //保存简历
        $(this).on('click', function () {

            edit();

            var $inputs = $(this).prev().find('input');
            var $textareas = $(this).prev().find('textarea');
            for (var i = 0, j = 0; i < $inputs.length || j < $textareas.length; i++, j++) {
                $inputs.eq(i).val($inputs.eq(i).val());
                $textareas.eq(j).val($textareas.eq(j).val());
            }
            $(this).html('编辑').prepend((element_small));
            $(this).prev().find('input,textarea').removeClass('edit-active');
            $(this).prev().find('input,textarea').attr('disabled', true);
            $(this).off();
            add_resume();
        });
    });

    function edit() {

        var resume = {};

        var id = '';
        var url1 = location.search;
        if (url1.indexOf("?") != -1) {
            id = url1.substr(4);
        }

        var resume_name = $('.resume-name div input')[0].value;//简历名称

        var photo = $('.head-portrait-content label img').attr('src');//头像

        var arr_inputs = $('.essential-information-content div label input');//基本信息
        var essential_info = '';
        var arr = ['userName', 'sex', 'education', 'working', 'birth', 'address', 'tel', 'email'];
        for (var i1 = 0; i1 < arr_inputs.length; i1++) {
            essential_info += arr[i1] + ":" + arr_inputs[i1].value + ",";
        }
        var work_experience_time = $('.work-experience-content ul li div h6 input');//工作经验
        var work_experience_content = $('.work-experience-content ul li div textarea');
        var work_experience = '';
        for (var i2 = 0; i2 < work_experience_time.length; i2++) {
            work_experience += work_experience_time[i2].value + '#' + work_experience_content[i2].value + ',';
        }

        var hope_job_content = $('.help-job div h6 input');//期望工作
        var hope_job_details = $('.help-job div textarea');
        var hope_job = hope_job_content[0].value + '#' + hope_job_details[0].value;

        var self_introduction = $('.self-introduction div textarea')[0].value;//自我评价

        resume = {
            "id": id,
            "resumeName": resume_name,
            "photo": photo,
            "essentialInfo": essential_info,
            "workExp": work_experience,
            "expectPost": hope_job,
            "selfAssessment": self_introduction
        };

        var url = server + '/server/resume/setUpResume.json';
        $.post(url, resume, function (data) {

        });

    }
}

/***********************************添加简历end***********************************/

/***********************************简历列表start***********************************/
function edit_resume_lists() {
    $('.edit-content').on('click', function () {
        $(this).html('取消');
        var edit_element = $('.edit');
        edit_element.slideDown();
        $(this).off();
        $(this).click(function () {
            $(this).html('编辑');
            edit_element.slideUp();
            edit_resume_lists();
        });
        //删除简历

        edit_element.find('.delete').on('click', function () {

            id = $(this).parent('.edit').prev().attr('href').split("=")[1];
            var url1 = server + '/server/resume/delete/byId.json';
            var args1 = {"resumeId": id};
            $.post(url1, args1, function (data) {
                if (data.code == 20000) {
                    alert("删除成功");
                }
            });

            $(this).parents('.list-box').slideUp(200);
            $('.edit-content').html('编辑');
            edit_element.slideUp(100);
            $('.delete').off();
        });
        //设置为默认简历
        $('.setup').off();
        edit_element.find('.setup').on('click', function () {

            id = $(this).parent('.edit').prev().attr('href').split("=")[1];
            var url2 = server + '/server/resume/set/default.json';
            var args2 = {"defaultResume": id};
            $.post(url2, args2, function (data) {
            });

            edit_element.find('.setup').html('设为默认');
            $(this).html('已为默认');
            $('.list-box>a>span').removeClass('glyphicon-ok');
            $(this).parents('.list-box').find('a>span').addClass('glyphicon-ok');
            $('.edit-content').html('编辑');
            $(this).parent().slideUp(200);
            edit_element.slideUp(100);

        });
    });
}

/***********************************简历列表end***********************************/
